#!/bin/bash

#
# (c) 2009 Mandriva, http://www.mandriva.com/
# (c) 2009 Jean-Dominique Frattini, jd.frattini@free.fr


# this uninstalls installed virtualbox hd and vm

vm_name="testcloud"

# This could provoke some problems if the real user having the rights to run
# testcloud isn't the first match user here...
# A solution would be to store the user somewhere (config dir ?)
user=`cat /etc/group | grep testcloud | awk -F ":" ' { print $4 } ' | awk -F "," ' { print $1 } '`
user_dir=`cat /etc/passwd | grep ${user} | awk -F ":" ' { print $6 } '`

if [ "${user}" = "" ]; then
   echo "No users were in the group testcloud"
   exit 1
fi

su - ${user} -c "VBoxManage closemedium disk ${vm_name}.vdi"
su - ${user} -c "VBoxManage modifyvm ${vm_name} --hda none"
su - ${user} -c "VBoxManage unregistervm ${vm_name} --delete"

rm -rf /usr/share/testcloud/*
rm -rf ${user_dir}/.testcloud/*

exit 0
